package com.zh.note.huawei.onehundred.整理扑克牌;

import java.util.*;

/**
 * @Author: zhanghuan
 * @date: 2023/8/4 09:35
 * @description: create it
 */
public class Main {
    /**
     * 难点：自定义排序规则
     *
     * @param args
     */
    public static void main(String[] args) {
        // Scanner sc = new Scanner(System.in);
        // String line = sc.nextLine();

        // String line = "4 4 2 1 2 1 3 3 3 4";
        // String line = "1 3 3 3 2 1 5";
        // String line = "4 4 4 1 3 5";
        String line = "1 3 6 9 4";
        String[] split = line.split(" ");

        //统计每个元素出现次数
        Map<Integer, Integer> map = new HashMap<>();
        for (String s : split) {
            int num = Integer.parseInt(s);
            map.put(num, map.getOrDefault(num, 0) + 1);
        }
        List<Map.Entry<Integer, Integer>> list = new ArrayList<>(map.entrySet());
        StringBuilder builder = new StringBuilder();

        while (list.size() > 0) {
            // 排序，自定义排序规则
            list.sort(new Comparator<Map.Entry<Integer, Integer>>() {
                @Override
                public int compare(Map.Entry<Integer, Integer> o1, Map.Entry<Integer, Integer> o2) {
                    //出现次数不同时，出现次数多的排在前面
                    if (!o2.getValue().equals(o1.getValue())) {
                        return o2.getValue() - o1.getValue();
                    } else {
                        // 出现次数相同时，key大的排在前面
                        return o2.getKey() - o1.getKey();
                    }

                }
            });

            Map.Entry<Integer, Integer> first = list.get(0);
            // 如果出现次数大于4
            if (first.getValue() >= 4) {
                append(builder, first.getKey(), 4);
                if (first.getValue() - 4 == 0) { // 如果刚好等于4，使用了，从list移除
                    list.remove(0);
                } else { // 如果大于4，使用了4张，求出剩余的张数
                    first.setValue(first.getValue() - 4);
                }
                continue;
            }
            //
            if (first.getValue() == 3 && list.size() > 1) {
                Map.Entry<Integer, Integer> second = list.get(1);
                if (second.getValue() >= 2) {
                    append(builder, first.getKey(), 3);
                    append(builder, second.getKey(), 2);
                    if (second.getValue() == 2) {
                        list.remove(1);
                    } else {
                        second.setValue(second.getValue() - 2);
                    }
                    list.remove(0);
                    continue;
                } else {
                    append(builder, first.getKey(), 2);
                    first.setValue(first.getValue() - 2);
                    continue;
                }
            }
            int size = list.size();
            for (int i = 0; i < size; i++) {
                append(builder, list.get(0).getKey(), list.get(0).getValue());
                list.remove(0);
            }

        }
        System.out.println(builder.subSequence(0, builder.length() - 1));
    }

    private static void append(StringBuilder builder, int num, int count) {
        for (int i = 0; i < count; i++) {
            builder.append(num).append(" ");
        }

    }
}
